package com.xcesys.extras.remoting.system.impl;

import java.util.ArrayList;
import java.util.Collection;

import javax.jws.WebService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xcesys.extras.core.service.ICrudService;
import com.xcesys.extras.dataobject.system.CodeList;
import com.xcesys.extras.dataobject.system.CodeType;
import com.xcesys.extras.model.TcCodeList;
import com.xcesys.extras.model.TcCodeType;
import com.xcesys.extras.remoting.BaseRemotingServiceCrudImpl;
import com.xcesys.extras.remoting.system.RCodeListService;
import com.xcesys.extras.system.service.TcCodeListService;
import com.xcesys.extras.system.service.TcCodeTypeService;

@Service("RCodeListService")
@WebService(endpointInterface = "com.xcesys.extras.remoting.system.RCodeListService")
public class RCodeListServiceImpl extends
		BaseRemotingServiceCrudImpl<CodeList, TcCodeList> implements
		RCodeListService {
	@Autowired
	private TcCodeListService service = null;

	@Autowired
	private TcCodeTypeService codeTypeService = null;

	@Override
	public CodeType doCreateCodeType(CodeType codeType) {
		return fromModel(CodeType.class, codeTypeService.create(fromDataObject(
				TcCodeType.class, codeType)));
	}

	@Override
	public void doDeleteCodeList(CodeList codelist) {
		service.delete(fromDataObject(codelist));
	}

	@Override
	public void doDeleteCodeType(CodeType codeType) {
		codeTypeService.delete(fromDataObject(TcCodeType.class, codeType));
	}

	@Override
	public Collection<CodeList> doFindAllCodeLists() {
		Collection<CodeList> result = fromModel(service.findAll());
		return result;

	}

	@Override
	public Collection<CodeType> doFindAllCodeTypes() {
		Collection<CodeType> result = fromModel(CodeType.class,
				codeTypeService.findAll());
		return result;
	}

	@Override
	public CodeList doFindCodeListById(Long id) {
		CodeList result = fromModel(service.findById(id));
		return result;

	}

	@Override
	public Collection<CodeList> doFindCodeLists(String type) {
		Collection<CodeList> result = fromModel(service.findByType(type));
		return result;

	}

	@Override
	public CodeType doFindCodeTypeById(Long id) {
		CodeType result = fromModel(CodeType.class,
				codeTypeService.findById(id));
		return result;

	}

	@Override
	public Collection<CodeType> doFindCodeTypesByType(String type) {
		if (type != null && !"".equals(type)) {
			TcCodeType codeType = codeTypeService.findByName(type);
			ArrayList<CodeType> result = new ArrayList<CodeType>();
			result.add(fromModel(CodeType.class, codeType));
			return result;
		} else {
			return fromModel(CodeType.class, codeTypeService.findAll());
		}
	}

	@Override
	public CodeList doUpdateCodeList(CodeList codelist) {
		// TcCodeList tcCodeList = codeListService.doFindById(codelist.getId());
		TcCodeList tcCodeList = fromDataObject(codelist);
		service.update(tcCodeList);
		return codelist;
	}

	@Override
	public CodeType doUpdateCodeType(CodeType codeType) {
		TcCodeType tcCodeType = codeTypeService.update(fromDataObject(
				TcCodeType.class, codeType));
		return fromModel(CodeType.class, tcCodeType);
	}

	@Override
	protected ICrudService<TcCodeList> getCrudService() {
		return service;
	}

}
